<?php

declare(strict_types=1);
/**
 * @author chenshubo
 * 数据库模型基类
 */
namespace App\Model;

use Hyperf\DbConnection\Db;
use Hyperf\DbConnection\Model\Model;

class BaseModel extends Model
{
    public $timestamps = false;
    protected static $instance;
    public static function instance()
    {
        $class_name = get_called_class();
    
        if (empty(self::$instance[$class_name])) {
            self::$instance[$class_name] = new static();
        }
        return self::$instance[$class_name];
    }
    
    /**
     * 获取单行数据
     * @param array $where
     */
    public function info( $where = [], $select = ['*'] ) 
    {
        $info = $this->where($where)->select($select)->first();
        if( empty($info) ) {
            return [];
        }
        
        return $info->toArray();
    }
    
    /**
     * 获取N行数据
     * @param array $where
     */
    public function rows( $where = [], $select = ['*'] )
    {        
        $query = $this->_dealWhere($where);
        return $query->select($select)->get()->toArray();
    }
    

    /**
     * @按页获取数据
     * @param    array  $where
     * @param    int    $limit
     * @param    int    $page
     * @access   public
     * @return   array
     */
    public function listPages($where = [], $limit = 20, $page = 1)
    {
        $db = $this->_dealWhere($where);
    
        $offset = $limit * ($page - 1);
        $offset < 0 && $offset = 0;
        $limit <= 0 && $limit = 20;
    
        $db->limit($offset, $limit);
    
        $lists  =  $db->select();
    
        if (empty($lists)) {
            return [];
        }
    
        return $lists->toArray();
    }
    
    /**
     * 处理where 条件
     */
    public function _dealWhere( $where = [] ) 
    {
        //group
        if( isset($where['_group_']) ) {
            $this->groupBy($where['_group_']);
        }
        
        //order by
        if( isset($where['_order_']) ) {
            if (is_array($where['_order_'])) {
                foreach ($where['_order_'] as $key => $value) {
                    $this->orderBy($key,$value);
                }
            } else {
                $this->orderBy($where['_order_']);
            }
        }
        
        //限制开始页码
        if(isset($where['_page_']) && is_numeric($where['_page_'])){
            $this->offset($params['_page_']);
        }
        
        //限制数据条数
        if(isset($where['_limit_']) && is_numeric($where['_limit_'])){
            $this->limit($params['_limit_']);
        }
        return self::query()->where($where);
    }
    
    /**
     * 写入单行数据
     * @param array $where
     */
    public function add( $arr = [] ) 
    {
        if( empty($arr) ) {
            return false;
        }
        
        return $this->insertGetId($arr);
    }
    
    /**
     * 写入多行单行数据
     * @param array $where
     */
    public function addMore( $arr = [] )
    {
        if( empty($arr) ) {
            return false;
        }
        
        return $this->insert($arr);
    }
    
    /**
     * 写入单行数据
     * @param array $where
     */
    public function edit( $where = [] ,$arr = [] )
    {
        if( empty($where) || empty($arr) ) {
            return false;
        }
    
        return $this->where($where)->update($arr);
    }

    /**
     * 删除数据
     * @param array $where
     */
    public function del( $where = [] )
    {
        if( empty($where) ) {
            return false;
        }
    
        return $this->where($where)->delete();
    }
    
    /**
     * 开启事务
     */
    public function startTrans()
    {
        Db::beginTransaction();
    }
    
    /**
     * 提交事务
     */
    public function commit()
    {
        Db::commit();
    }
    
    /**
     * 回滚事务
     */
    public function rollback()
    {
        Db::rollBack();
    }
}
